# To change this template, choose Tools | Templates
# and open the template in the editor.

class CosineSim
  def initialize
    
  end
  # moltiplica tra loro le componenti di un vettore
  def vector_mult (vector_x, vector_y)
    vector_mult= 0
    (0..vector_x.size-1).each{|x|
      vector_mult= vector_mult + vector_x[x]*vector_y[x]
    }
    
    return vector_mult
  end
    def vector_square_sum (vector_x)
    vector_square_sum= 0
    vector_x.each{|x|
      vector_square_sum= vector_square_sum+ (x**2)
    }

    return vector_square_sum
  end
  def vector_sq_sum (vector_x,vector_y)

    vector_sq_sum= 0
    vector_x_sum= self.vector_square_sum(vector_x)
    vector_y_sum= self.vector_square_sum(vector_y)

      vector_sq_sum= Math.sqrt(vector_x_sum)*Math.sqrt(vector_y_sum)

 
    return vector_sq_sum
  end
    # funzione per la coseno similarità. input: vettori di rating degli utenti
  def cosine_similarity(vector_x, vector_y)
    cos_sim= 0

    vector_x_m= self.vector_mult(vector_x, vector_y)
    # vector_y_m= self.vector_mult(vector_y)

    vector_sq_sum= self.vector_sq_sum(vector_x, vector_y)

    cos_sim= (vector_x_m)/vector_sq_sum
    
    return cos_sim
  end
  # funzione di coseno similarità che ritorna un vettore di similarità
  def similarity (utente, utenti)
    sim= []

    (0..utenti.size-1).each {|i|
      sim.push(self.cosine_similarity(utente.no_context, utenti[i].no_context))
    }

    return sim
  end
  def vector_matrix(matrix, vector)
    sim=[]
        (0..matrix.size-1).each {|i|
      sim.push(self.cosine_similarity(vector, matrix[i]))
    }
    return sim
  end
  def normalize(sim_vector)
    normal= []

    sim_vector.each { |x|
      normal.push(x/(sim_vector.max{ |x,y| x <=> y }))}
  end
end
